package com.example.controller;

import com.example.entity.result.Result;
import com.example.entity.result.ResultBean;
import com.example.entity.vo.Order;
import com.example.entity.vo.State;
import com.example.service.OrderService;
import com.example.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/order")
@RestController
public class OrderController extends BaseHandleException {
    @Autowired
    private OrderService orderService;

    //添加订单
    @RequestMapping("/insert")
    public ResultBean insertOrder(@RequestParam("order") String json) {
        Order order = DataUtils.getOrder(json);
        Result result = orderService.insertOrder(order);
        return DataUtils.getResultBean(result);
    }

    //修改订单
    @RequestMapping("/update")
    public ResultBean updateOrder(@RequestParam("order") String json) {
        Order order = DataUtils.getOrder(json);
        Result result = orderService.updateOrder(order);
        return DataUtils.getResultBean(result);
    }


    //取消订单
    @RequestMapping("/cancel")
    public ResultBean CancelOrder(@RequestParam("order") String json) {
        Order order = DataUtils.getOrder(json);
        Result result = orderService.setOnState(order, State.CANCEL);
        return DataUtils.getResultBean(result);
    }

    //设置订单状态
    @RequestMapping("/setState")
    public ResultBean setOrderState(@RequestParam("order") String json, @RequestParam("state") String state) {
        Order order = DataUtils.getOrder(json);
        if (!State.isState(state)) {
            return DataUtils.getResultBean(Result.PARAMETER_INVALID, "参数无效");
        }
        Result result = orderService.setOnState(order, State.toState(state));
        System.out.println(state);
        System.out.println(result);
        return DataUtils.getResultBean(result);
    }

}
